package com.chixing.controller;

import com.chixing.mapper.ProductMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.Product;
import com.chixing.service.CustomerService;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CustomerController {

  @Autowired
  private CustomerService customerService;

  @PostMapping("/customer/login")
  public String login(Long custumerTel, String custumerPsw, HttpSession session) {
    ServerResult serverResult = customerService.login(custumerTel,custumerPsw);
    System.out.println("controller --- cuspsw" + custumerPsw);
    System.out.println("controller --- cusTel" + custumerTel);

    if (serverResult.getCode() == 200) {
      System.out.println("登录成功！");
      Customer data = (Customer) serverResult.getData();
      session.setAttribute("loginCustomer", data);
      return "redirect:/index.jsp";
    } else {
      System.out.println("登录失败！");
      return "customer/login";
    }
  }

  //退出
  @GetMapping("/customer/logout")
  public String logout(HttpSession session) {
    session.removeAttribute("loginCustomer");
    return "redirect:/index.jsp";
  }

  @PostMapping("/customer")
  @ResponseBody
  public ModelAndView regist(Customer customer) {
    System.out.println("custName:" + customer.getCustumerName());
    System.out.println("password:" + customer.getCustumerPsw());
//    System.out.println("custId:" + customer.getCustumerId());
    ModelAndView modelAndView = new ModelAndView();


    System.out.println("从页面中获得的密码是：" + customer.getCustumerPsw());
    ServerResult regist = customerService.regist(customer);

    modelAndView.setViewName("/customer/login");
    return modelAndView ;

  }

  @GetMapping("/customer/{id}")
  public ModelAndView getById(HttpSession session) {

    Customer loginCustomer = (Customer)session.getAttribute("loginCustomer");
    Integer custumerId = loginCustomer.getCustumerId();
    ModelAndView mav = new ModelAndView();
    //通过id查询用户
    ServerResult result = customerService.getById(custumerId);
    if (result.getCode() == 200) {
      System.out.println("controller --- 通过主键查询用户信息成功。");

      //数据绑定
      mav.addObject("customer" , result.getData());
      //页面跳转
      mav.setViewName("customer/cus_detail");

    }

    return mav;

  }

  @PutMapping("/customer")
  public String updateById(Customer customer) {

    System.out.println("需要更新的用户为：" + customer);
    ServerResult result = customerService.update(customer);
//    ModelAndView mav = new ModelAndView();

    System.out.println("update data" + result.getCode());

    if (result.getCode() == 200) {
      System.out.println("修改成功！");
      return "redirect:/customer/" + customer.getCustumerId();
    } else { //修改失败 ， 继续留在修改 表单页面

      System.out.println("修改失败！");

      return "customer/cus_detail.jsp";

    }

  }

  @GetMapping("/publickey")
  @ResponseBody
  public ServerResult resregister() {
    return customerService.getPublicKey();
  }

}
